package surf.browser;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.swing.JFrame;

/**
 * The main class of the program. Handles the web browser.
 * @author Tom Cashman
 *
 */
public class WebBrowser extends JFrame implements WindowListener {
	private static final long serialVersionUID = 1L;
	private TabManager m_tabManager;
	private Settings m_settings;
	
	/**
	 * Default constructor. Initialised the frame and the browser.
	 */
	public WebBrowser() {
		//Set up the frame and it's contents
		super("Surf@Home");
		this.addWindowListener(this);
		this.getContentPane().setLayout(new BorderLayout());
		m_settings = new Settings();
		m_tabManager = new TabManager(this);
		
		this.getContentPane().add(m_tabManager, BorderLayout.PAGE_START);
		this.setSize(800, 600);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	/**
	 * Called when the window is closing. Safely closes all browser tabs (Not doing this causes jvm crash).
	 */
	public void windowClosing(WindowEvent e) {
        while(m_tabManager.getRootTab() != null) {
        	m_tabManager.close(m_tabManager.getRootTab().getTab());
        }
    }
	
	/**
	 * Main method.
	 * @param args
	 */
	public static void main(String [] args) {
		//Detect the OS and set the native path to xulrunner
		String path = new File(".").getAbsolutePath() + "/";
		if(System.getProperties().getProperty("os.name").contains("Windows")) {
			System.setProperty("java.library.path",path + "native/win32/");
		} else if(System.getProperties().getProperty("os.name").contains("Linux")) {
			System.setProperty("java.library.path",path + "native/linux/");
		} else {
			System.setProperty("java.library.path",path + "native/linux/");
		}
		//Start the web browser
		WebBrowser wb = new WebBrowser();
	}

	public void windowActivated(WindowEvent arg0) {}

	public void windowClosed(WindowEvent arg0) {}

	public void windowDeactivated(WindowEvent arg0) {}

	public void windowDeiconified(WindowEvent arg0) {}

	public void windowIconified(WindowEvent arg0) {}

	public void windowOpened(WindowEvent arg0) {}
}
